/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.servlet.home;

import com.zjdt.epserver.db.service4mysql.UserServiceImpl;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author dusin
 */
@WebServlet(name = "WebLogin", urlPatterns = {"/servlet/home/Login"})
public class Login extends HttpServlet {
    private UserServiceImpl instance;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        boolean sucess = false;
        String msg = "用户名密码错误";
        HttpSession session = request.getSession();
        try {            
           String username = request.getParameter("username");
           String password = request.getParameter("password");
           
           ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
           instance = (UserServiceImpl) context.getBean("userService");
           Map<String,Object> map = instance.login(username, password);
           if(map!=null&&"super".equals(username)){
                session.setAttribute("username", username);
                session.setAttribute("userid", map.get("ID"));           
                session.setAttribute("name", map.get("name"));
                session.setAttribute("phoneNumber", map.get("mobilePhone"));
                //String s = (String)map.get("mobilePhone");
                //System.out.println(s+" *****");
                session.setAttribute("url", "../main/bussiness.jsp");//首页
                response.sendRedirect("../../jsp/main/adminmain.jsp");
           }else if(map!=null){
               session.setAttribute("username", username);
                session.setAttribute("userid", map.get("ID"));           
                session.setAttribute("name", map.get("name"));
                session.setAttribute("phoneNumber", map.get("mobilePhone"));
                //String s = (String)map.get("mobilePhone");
                //System.out.println(s+" *****");
                List<Map<String,Object>> modules = instance.findAllModules(username);
                session.setAttribute("modules", modules);
                if(modules==null||modules.isEmpty()){
                    session.setAttribute("url", "../home/nomodule.jsp");
                }else{
                    String firstUrl = (String)modules.get(0).get("url");
                    session.setAttribute("url", ".."+firstUrl);//选择该用户的第一个页面
                }
                response.sendRedirect("../../jsp/main/usermain.jsp");
           }else{
                session.setAttribute("errmsg", msg);            
                response.sendRedirect("../../index.html");
           }
        } catch(EmptyResultDataAccessException e){
            session.setAttribute("errmsg", msg);
            /*String json = TheUtil.getJsonResult(sucess, msg);            
            out.println(json);*/
            response.sendRedirect("../../index.html");
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
